/*
 * PreviewSQL.java
 *
 * Created on Utorok, 2008, marec 18, 23:29
 */
package feedback;

import com.Ostermiller.Syntax.HighlightedDocument;
import db.definitions.DatabaseDefinition;
import db.execute.Connect;
import java.awt.Graphics;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;

/**
 *
 * @author  abracadabuda
 */
public class PreviewSQL extends javax.swing.JDialog {

    private Connect connection;
    private String[] partsOfSql = new String[DatabaseDefinition.SQL_LAST + 1];
    private HighlightedDocument document = new HighlightedDocument();

    /** Creates new form PreviewSQL */
    public PreviewSQL(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        document.setHighlightStyle(HighlightedDocument.SQL_STYLE);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        executeButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        previewTextField = new JTextPane(document);
        akciaLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(feedback.FeedBackApp.class).getContext().getResourceMap(PreviewSQL.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        executeButton.setText(resourceMap.getString("executeButton.text")); // NOI18N
        executeButton.setName("executeButton"); // NOI18N
        executeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                executeButtonActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        previewTextField.setEditable(false);
        previewTextField.setFont(new java.awt.Font("Courier New", 0, 12));
        previewTextField.setName("previewTextField"); // NOI18N
        jScrollPane1.setViewportView(previewTextField);

        akciaLabel.setText(resourceMap.getString("akciaLabel.text")); // NOI18N
        akciaLabel.setName("akciaLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 857, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(akciaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 754, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(executeButton)
                        .addGap(24, 24, 24))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(executeButton)
                    .addComponent(akciaLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void executeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_executeButtonActionPerformed
        executeButton.setEnabled(false);
        try {
            for (int i = DatabaseDefinition.SQL_FIRST - 1; i < DatabaseDefinition.SQL_LAST; i++) {
                connection.execute(partsOfSql[i]);
                akciaLabel.setText(DatabaseDefinition.getNameOfType(i));
                Graphics g = akciaLabel.getGraphics();
                g.clearRect(0, 0, akciaLabel.getWidth(), akciaLabel.getHeight());
                akciaLabel.setText(DatabaseDefinition.getNameOfType(i));
                akciaLabel.paint(g);
            }
            akciaLabel.setText("Done");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(PreviewSQL.class.getName()).log(Level.SEVERE, null, ex);
        }      
//        try {
//            connection.execute(previewTextField.getText());
//        } catch (SQLException ex) {
//            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
//            Logger.getLogger(PreviewSQL.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_executeButtonActionPerformed

    public void setPreviewText(String sql) {
        previewTextField.setVisible(false);
        previewTextField.setText(sql);
        previewTextField.setCaretPosition(0);
        previewTextField.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel akciaLabel;
    private javax.swing.JButton executeButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane previewTextField;
    // End of variables declaration//GEN-END:variables
    public Connect getConnection() {
        return connection;
    }

    public void setConnection(Connect connection) {
        this.connection = connection;
    }

    public String[] getPartsOfSql() {
        return partsOfSql;
    }
}
